package com.sphd.Restaurant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sphd.Restaurant.R;
import com.sphd.Restaurant.base.BaseActivity;
import com.sphd.Restaurant.infos.TopupInfo;
import com.sphd.Restaurant.model.Model;
import com.sphd.Restaurant.presenter.RechargeCardP;
import com.sphd.Restaurant.utils.AppManager;
import com.sphd.Restaurant.utils.Constants;
import com.sphd.Restaurant.views.RechargeCardView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RechargeLogActivity extends BaseActivity implements RechargeCardView {
    Intent intent;
    Bundle bundle;
    TopupInfo topupInfo;
    @BindView(R.id.into_cg)
    ImageView intoCg;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.my_srf)
    SwipeRefreshLayout mySrf;
    BaseQuickAdapter<TopupInfo> baseQuickAdapter;
    List<TopupInfo> couponInfoList;
    RechargeCardP rechargeCardP;
    int curpage = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge_log);
        AppManager.getAppManager().addActivity(this);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        couponInfoList = new ArrayList<>();
        rechargeCardP = new RechargeCardP(this);
        topupInfo = (TopupInfo) bundle.getSerializable(Constants.TOUP);
        setToolBar(toolbar, topupInfo.getMem_cgname(), true);
        setRefresh(mySrf);
        setRecy(myRecy);
        initAdapter();
        initEvent();
        addData();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(view -> AppManager.getAppManager().finishActivity(this));
        baseQuickAdapter.setOnLoadMoreListener(() -> {
            curpage++;
            addData();
        });
        mySrf.setOnRefreshListener(() -> {
            curpage=1;
            addData();
        });
    }

    //TODO 已更换链接 2016/10/14
    private void addData() {
        rechargeCardP.addData(Model.HTTPURL+ Model.RECHARGELOG+"username="+getString(Constants.USERPHONE)+"&cgid="
                +topupInfo.getMem_cgid()+"&curpage="+curpage+"&pageno=10");
//        rechargeCardP.addData("http://restaurant.zhongxs.com/Restaurant/cg/client_recordtopup.action?username=18611833553&cgid=257&curpage=1&pageno=10");
    }

    private void initAdapter() {
        baseQuickAdapter = new BaseQuickAdapter<TopupInfo>(R.layout.item_recharge, couponInfoList) {
            @Override
            protected void convert(BaseViewHolder helper, TopupInfo item) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm");
                Date d1 = new Date(item.getRegtime());
                helper.setText(R.id.tv_most_right, "充值金额" + turn(item.getTopup(), 2) + "元")
                        .setText(R.id.tv_most_lift, "充值时间" + format.format(d1));
            }
        };
        baseQuickAdapter.openLoadAnimation();
        baseQuickAdapter.openLoadMore(10);
        myRecy.setAdapter(baseQuickAdapter);
    }

    @OnClick(R.id.into_cg)
    public void onClick() {
        bundle.putString(Constants.TAG, RechargeLogActivity.class.getName());
        bundle.putString(Constants.CG_ID, String.valueOf(topupInfo.getMem_cgid()));
        startActivity(RestaurantPageActivity.class, bundle);
    }

    @Override
    public void sendList(List<TopupInfo> topupInfos) {
        if (curpage == 1) {
            baseQuickAdapter.setNewData(topupInfos);
        } else {
            baseQuickAdapter.addData(topupInfos);
        }
        if (topupInfos.size() != 10) {
            baseQuickAdapter.loadComplete();
            addFootView(baseQuickAdapter);
        }
    }

    @Override
    public void showdialog() {
        showDialog();
    }

    @Override
    public void dissmissdialog() {
        dissMissDialog();
        mySrf.setRefreshing(false);
    }
}
